package com.banach.prema.executor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.banach.prema.constant.MsgType;
import com.banach.prema.dto.cmd.AlarmAdd;
import com.banach.prema.dto.cmd.AlarmUpd;
import com.banach.prema.im.rest.GroupRestApi;
import com.banach.prema.repository.AlarmRepository;
import com.banach.prema.repository.dataobject.AlarmDO;
import com.ego.base.api.LocationRemoteService;
import com.ego.base.dto.vo.AddressVO;
import com.ego.framework.core.context.UserHolder;
import com.ego.framework.core.tookit.BeanToolkit;
import com.ego.framework.core.util.ClassUtils;
import com.ego.framework.core.util.Snowflake;
import com.ego.framework.dto.Response;
import com.ego.framework.model.LoginUser;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @author ajun
 *
 */
@RestController
public class AlarmPublishExe {
	@Resource
	private LocationRemoteService locationRemoteService;

	@Resource
	private GroupRestApi groupRestApi;

	@Resource
	private AlarmRepository alarmRepository;
	@Resource
	private Snowflake snowflake;

	/**
	 * 发布告警信息
	 * @param alarmAdd 告警添加对象，包含告警的基本信息
	 * @return Response 响应结果，包含发布成功的信息
	 */
	public Response publish(AlarmAdd alarmAdd) {
		// 获取当前登录用户信息
		LoginUser user = UserHolder.getLoginUser();
		// 根据经纬度获取地址信息
		AddressVO address = locationRemoteService.getAddress(alarmAdd.getLng(), alarmAdd.getLat()).getData();

		// 将告警添加对象转换为告警数据对象并设置相关属性
		final AlarmDO alarm = BeanToolkit.getInstance().copy(alarmAdd, AlarmDO.class);
		ClassUtils.assign(alarm, address);
		alarm.setId(snowflake.nextId());
		alarm.setUserId(user.getId());
		alarm.setCreateTime(LocalDateTime.now());
		alarm.setUpdateTime(LocalDateTime.now());
		// 创建告警群组并保存告警信息
		final String groupId = groupRestApi.createAlarmGroup(alarmAdd);
		alarm.setGroupId(groupId);
		alarmRepository.save(alarm);
		// 发送IM消息通知
		sendImMsg(address, alarmAdd.getContent(), groupId);
		return Response.success("发布成功");
	}


	/**
	 * 重新发布告警信息
	 * @param alarmUpd 告警更新对象，包含更新后的告警信息
	 * @return Response 响应结果，包含发布成功的信息
	 */
	public Response republish(AlarmUpd alarmUpd) {
		// 获取当前登录用户信息
		LoginUser user = UserHolder.getLoginUser();
		// 根据经纬度获取地址信息
		AddressVO address = locationRemoteService.getAddress(alarmUpd.getLng(), alarmUpd.getLat()).getData();

		// 将告警更新对象转换为告警数据对象并设置相关属性
		final AlarmDO alarm = BeanToolkit.getInstance().copy(alarmUpd, AlarmDO.class);
		ClassUtils.assign(alarm, address);
		alarm.setUserId(user.getId());
		alarm.setCreateTime(LocalDateTime.now());
		alarm.setUpdateTime(LocalDateTime.now());
		// 创建告警群组并更新告警信息
		final String groupId = groupRestApi.createAlarmGroup(alarmUpd);
		alarm.setGroupId(groupId);
		alarmRepository.update(alarm);
		// 发送IM消息通知
		sendImMsg(address, alarmUpd.getContent(), groupId);
		return Response.success("发布成功");
	}


	/**
	 * 发送消息
	 * @param address
	 * @param groupId
	 */
	private void sendImMsg(AddressVO address,String msgText,String groupId) {
		LoginUser appUser = UserHolder.getLoginUser();
		String title = appUser.getName()+"在"+address.getSimpleAddr()+"发布风险预警";
		JSONArray msgBody = new JSONArray();
		JSONObject textMsg = new JSONObject();
		textMsg.put("MsgType", MsgType.TEXT);
		JSONObject msgContent = new JSONObject();
		msgContent.put("Text", msgText);
		textMsg.put("MsgContent", msgContent);
		msgBody.add(textMsg);
		JSONObject locationMsg = new JSONObject();
		locationMsg.put("MsgType", MsgType.LOCATION);
		JSONObject msgContent2 = new JSONObject();

		msgContent2.put("Desc", address.getSimpleAddr());
		msgContent2.put("Latitude", address.getLat());
		msgContent2.put("Longitude", address.getLng());
		locationMsg.put("MsgContent", msgContent2);
		msgBody.add(locationMsg);

		JSONObject offlinePushInfo = new JSONObject();
		offlinePushInfo.put("PushFlag", 0);
		offlinePushInfo.put("Desc", title);
		offlinePushInfo.put("Ext", title);

		JSONObject androidInfo = new JSONObject();
		androidInfo.put("Sound", "android.mp3");
		JSONObject apnsInfo = new JSONObject();
		apnsInfo.put("Sound", "apns.mp3");
		apnsInfo.put("BadgeMode", 0);
		apnsInfo.put("Title", title);
		offlinePushInfo.put("AndroidInfo", androidInfo);
		offlinePushInfo.put("ApnsInfo", apnsInfo);
		groupRestApi.sendMsg(appUser.getMobile(), groupId, msgBody,offlinePushInfo);
	}
}
